package com.zhuannimei.ui;

import org.json.JSONObject;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.zhuannimei.R;
import com.zhuannimei.net.RequestServerData;
import com.zhuannimei.utility.UiUtil;
import com.zhuannimei.utility.ZnmData;
import com.zhuannimei.view.XXButton;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class UserRegActivity extends BaseActivity  {
	@ViewInject(R.id.et_phoneNum)
	private EditText etPhoneNum;
	@ViewInject(R.id.et_password)
	private EditText etPsw;
	@ViewInject(R.id.et_email)
	private EditText etEmail;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.user_reg);
		ViewUtils.inject(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
		mTitlePane.setTitle("用户注册");
		mTitlePane.getLeftButton().setVisibility(View.GONE);
		mTitlePane.getRightButton().setVisibility(View.GONE);
	}

	@OnClick(R.id.bt_reg)
	public void regClick(View v) {
		final String userName = etPhoneNum.getText().toString().trim();
		final String passWord = etPsw.getText().toString().trim();
		String email = etEmail.getText().toString().trim();
		if (TextUtils.isEmpty(userName)) {
			UiUtil.showToast("手机号不能为空");
			return;
		}
		if (TextUtils.isEmpty(passWord)) {
			UiUtil.showToast("密码不能为空");
			return;
		}
		if (TextUtils.isEmpty(email)) {
			UiUtil.showToast("邮箱不能为空");
			return;
		}
		RequestParams params = new RequestParams();
		params.addBodyParameter("username", userName);
		params.addBodyParameter("password", passWord);
		params.addBodyParameter("email", email);
		RequestServerData.userReg(params, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				pd.cancel();
				String result = responseInfo.result;
				try {
					if (!TextUtils.isEmpty(result)) {
						JSONObject jobj = new JSONObject(result);
						String rCode = jobj.optString("code");
						String desc = jobj.optString("desc");
						if ("0".equals(rCode)) {
							String userId = jobj.optString("result");
							ZnmData.getUserData().saveUserUserId(userId);
							Intent intent = new Intent(UserRegActivity.this,
									ConversionActivity.class);
							UserRegActivity.this.startActivity(intent);
							UserRegActivity.this.finish();
						} else {
							UiUtil.showToast(desc);
						}
					}
				} catch (Exception e) {
					UiUtil.showToast("注册失败");
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(HttpException error, String msg) {
				UiUtil.showToast("注册失败<<" + msg);
			}

			@Override
			public void onStart() {
				super.onStart();
				pd.setMsg("正在注册，请稍候...");
				pd.show();
			}
		});

	}
}
